#include "stdafx.h"
#include "StringHelper.h"

#include "MD5.h"
#include "SHA1.h"

StringHelper::StringHelper(void)
{
}

StringHelper::~StringHelper(void)
{
}

LPSTR StringHelper::__UnicodeToUTF8(LPCWSTR source)
{
	LPSTR dest;
	int destlen;

	destlen = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);

	dest = new char[destlen + 1];
	memset(dest, 0, destlen + 1);

	destlen = WideCharToMultiByte(CP_UTF8, 0, source, -1, dest, destlen, NULL, NULL);
	return dest;
}

LPWSTR StringHelper::__UTF8ToUnicode(LPCSTR source)
{
	int destlen;
	wchar_t * dest;

	destlen = MultiByteToWideChar( CP_UTF8, 0, source,-1, NULL,0 ); 

	dest = new wchar_t[destlen + 1]; 
	memset(dest, 0, destlen + 1); 

	MultiByteToWideChar(CP_UTF8, 0, source, -1, (LPWSTR)dest, destlen); 
	return dest; 
}

LPSTR StringHelper::__UnicodeToANSI(LPCWSTR source)
{
	char* dest;
	int destlen;

	destlen = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL );

	dest =new char[destlen+1];
	memset( dest, 0, destlen + 1);

	WideCharToMultiByte(CP_ACP, 0, source, -1, dest, destlen, NULL, NULL );
	return dest;
}

LPSTR StringHelper::__UTF8ToANSI(LPCSTR source)
{
	LPTSTR temp = __UTF8ToUnicode(source);
	LPSTR r = __UnicodeToANSI((LPTSTR)temp);
	delete [] temp;
	return r;
}

LPWSTR StringHelper::__ANSIToUnicode(LPCSTR source)
{
	int    destlen ;
	wchar_t * dest;

	destlen = MultiByteToWideChar( CP_ACP, 0, source, -1, NULL,0 );

	dest = new wchar_t [destlen + 1]; 
	memset(dest, 0, destlen + 1); 

	MultiByteToWideChar(CP_ACP, 0, source, -1, (LPWSTR)dest, destlen ); 

	return dest; 
}

void StringHelper::Free(LPCWSTR source)
{
	delete [] source;
}

void StringHelper::Free(LPCSTR source)
{
	delete [] source;
}

void SHA1Digest(char* msg, unsigned char* output, int l)
{
	sha1_context ctx;  
	SHA1::sha1_starts( &ctx ); 
	SHA1::sha1_update( &ctx, (uint8 *) msg, l ); 
	SHA1::sha1_finish( &ctx, output ); 
	output[20] = 0;
}

void MD5Digest(char *msg, unsigned char* output, int l)
{
	MD5 md5;
	md5.MD5Update((uint8 *) msg, l);
	md5.MD5Final ((uint8 *) output);
	output[16] = 0;
}

void StringHelper::MD5(char *msg, char *output, int l, bool uppercase)
{
	unsigned char  digest[17];
	MD5Digest(msg, digest, l);
	if(uppercase)
	{
		for(int j = 0; j < 16; j++ ) 
		{ 
			sprintf( output + j * 2, "%02X", digest[j] ); 
		} 
	}
	else
	{
		for(int j = 0; j < 16; j++ ) 
		{ 
			sprintf( output + j * 2, "%02x", digest[j] ); 
		} 
	}
}

void StringHelper::GetHostAndPageFromUrl(LPTSTR lpszURL, LPTSTR *lpszHost, LPTSTR *lpszPage)
{
	int j = 0;
	int l = _tcslen(lpszURL);

	for (int i = 0; i < l; i++)
	{
		if (lpszURL[i] == _T('/'))
			j++;

		switch (j)
		{
		case 2:
			*lpszHost = lpszURL + i + 1;
			j++;
			break;

		case 4:
			lpszURL[i] = _T('\0');
			*lpszPage = lpszURL + i + 1;
			return;
		}
	}
}

LPTSTR StringHelper::GetSidFromUri(LPCTSTR lpszUri)
{
	LPTSTR uri = _tcsdup(lpszUri);
	LPTSTR sid = NULL;

	sid = _tcstok(uri, _T("@"));
	sid = _tcschr(sid, _T(':')) + 1;

	sid = _tcsdup(sid);

	free(uri);
	return sid;
}

LPTSTR StringHelper::GetCookieVal(LPTSTR lpszCookie, LPTSTR cookieName)
{
	LPTSTR cookieVal, cookies, name, value;

	cookieVal = NULL;

	cookies = _tcsdup(lpszCookie);

	for(name = _tcstok(cookies, _T(";")); name; name = _tcstok(NULL, _T(";")))
	{
		while(_istspace((wint_t)name)) name++;

		value = _tcschr(name, _T('=')) + 1;

		*(value - 1)='\0';

		if(!_tcscmp(name, cookieName))
		{
			if(!(cookieVal = _tcsdup(value)))
			{
				free(cookies);
				return NULL;
			}
			break;
		}
	}

	free(cookies);

	return cookieVal;
}